package com.ooqiu.gaming.ui.dialog;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.ooqiu.gaming.R;
import com.ooqiu.gaming.utils.SizeUtils;

/**
 * @author Eenie
 * @date 2017/12/5
 */

public class SimpleEditDialog extends BaseBottomDialog implements View.OnClickListener {

    private EditText mEditContent;
    private TextView mTvtitle;
    private Button mBtnCancel;
    private Button mBtnEnter;
    private String title;
    private OnClickListener mListener;

    public static SimpleEditDialog newInstance() {
        SimpleEditDialog f = new SimpleEditDialog();
        return f;
    }


    public SimpleEditDialog setTitle(String title) {
        this.title = title;
        return this;
    }

    public SimpleEditDialog setListener(OnClickListener listener) {
        mListener = listener;
        return this;
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_simple_edit;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void bindView(View v) {
        mTvtitle = v.findViewById(R.id.tv_title);
        mBtnCancel = v.findViewById(R.id.btn_cancel);
        mBtnEnter = v.findViewById(R.id.btn_enter);
        mBtnCancel.setOnClickListener(this);
        mBtnEnter.setOnClickListener(this);

        if (TextUtils.isEmpty(title)) {
            mTvtitle.setVisibility(View.GONE);
        } else {
            mTvtitle.setVisibility(View.VISIBLE);
            mTvtitle.setText(title);
        }

        mEditContent = v.findViewById(R.id.edit_content);
        mEditContent.post(new Runnable() {
            @Override
            public void run() {
                InputMethodManager imm =
                        (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(mEditContent, 0);
            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        Window window = getDialog().getWindow();
        WindowManager.LayoutParams params = window.getAttributes();

        params.dimAmount = getDimAmount();
        params.width = WindowManager.LayoutParams.MATCH_PARENT + SizeUtils.dip2px(getContext(), 300);
        if (getHeight() > 0) {
            params.height = getHeight();
        } else {
            params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        }
        params.gravity = Gravity.CENTER;
        window.setAttributes(params);
    }

    @Override
    public void onClick(View v) {

        switch (v.getId()) {
            case R.id.btn_cancel:
                dismiss();
                break;
            case R.id.btn_enter:
                if (mListener != null) {

                    if (TextUtils.isEmpty(mEditContent.getText())) {
                        mEditContent.setError("内容不能为空");
                        return;
                    }
                    mListener.onEnter(mEditContent.getText().toString());
                    dismiss();
                }

                break;
        }
    }


    public interface OnClickListener {
        void onEnter(String content);
    }

}
